package com.cskaoyan.service.wx_search;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.cskaoyan.bean.po.wx_search.WxClearHistoryPO;
import com.cskaoyan.bean.po.wx_search.WxSearchHistoryPO;
import com.cskaoyan.bean.po.wx_search.WxSearchIndexDefaultKeywordPO;
import com.cskaoyan.bean.vo.wx_search.WxSearchHistoryVO;
import com.cskaoyan.bean.vo.wx_search.WxSearchIndexDefaultKeywordVO;
import com.cskaoyan.bean.vo.wx_search.WxSearchIndexVO;
import com.cskaoyan.mapper.wx_search.WxClearHistoryMapper;
import com.cskaoyan.mapper.wx_search.WxSearchDefaultKeywordMapper;
import com.cskaoyan.mapper.wx_search.WxSearchHistoryMapper;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Author: 冯子靖
 * @Description:
 * @Date: 2022/7/20 20:23
 */
@Service
public class WxSearchServiceImpl implements WxSearchService {

    @Autowired
    WxSearchDefaultKeywordMapper wxSearchDefaultkeywordMapper;

    @Autowired
    WxSearchHistoryMapper wxSearchHistoryMapper;

    @Autowired
    WxClearHistoryMapper wxClearHistoryMapper;

    /**
     * 搜索界面
     * @param
     * @return
     */
    @Override
    public WxSearchIndexVO printIndex(Integer userId) {
        /*
        defaultKeyword,默认关键字在数据库中数据为1
         */
        QueryWrapper<WxSearchIndexDefaultKeywordPO> wxSearchIndexDefaultKeywordPOQueryWrapper = new QueryWrapper<>();
        wxSearchIndexDefaultKeywordPOQueryWrapper.eq("is_default", 1);
        wxSearchIndexDefaultKeywordPOQueryWrapper.eq("deleted", 0);
        WxSearchIndexDefaultKeywordPO defaultKeyword = wxSearchDefaultkeywordMapper.selectOne(wxSearchIndexDefaultKeywordPOQueryWrapper);
        /*
        historyKeywordList.通过userId查询,需要token
        查询最近search的8条
         */
        QueryWrapper<WxSearchHistoryPO> wxSearchHistoryPOQueryWrapper = new QueryWrapper<>();

        wxSearchHistoryPOQueryWrapper.eq("user_id", userId);
        wxSearchHistoryPOQueryWrapper.eq("deleted", 0);
        wxSearchHistoryPOQueryWrapper.orderByDesc("update_time");
        wxSearchHistoryPOQueryWrapper.last("limit 8");
        List<WxSearchHistoryPO> historyKeywordList = wxSearchHistoryMapper.selectList(wxSearchHistoryPOQueryWrapper);

        /*
        hotKeywordList,热门在数据库中数据为1
         */
        QueryWrapper<WxSearchIndexDefaultKeywordPO> wxSearchIndexDefaultKeywordPOQueryWrapper1 = new QueryWrapper<>();
        wxSearchIndexDefaultKeywordPOQueryWrapper1.eq("is_hot", 1);
        wxSearchIndexDefaultKeywordPOQueryWrapper1.eq("deleted", 0);
        wxSearchIndexDefaultKeywordPOQueryWrapper1.orderByDesc("update_time");
        wxSearchIndexDefaultKeywordPOQueryWrapper1.last("limit 8");
        List<WxSearchIndexDefaultKeywordPO> hotKeywordList = wxSearchDefaultkeywordMapper.selectList(wxSearchIndexDefaultKeywordPOQueryWrapper1);


        WxSearchIndexDefaultKeywordVO wxSearchIndexDefaultKeywordVO = new WxSearchIndexDefaultKeywordVO();
        List<WxSearchHistoryVO> wxSearchHistoryVOS = new ArrayList<>();
        List<WxSearchIndexDefaultKeywordVO> wxSearchIndexDefaultKeywordVOS = new ArrayList<>();
        try {
            BeanUtils.copyProperties(wxSearchIndexDefaultKeywordVO, defaultKeyword);

            for (WxSearchHistoryPO wxSearchHistoryPO : historyKeywordList) {
                WxSearchHistoryVO wxSearchHistoryVO = new WxSearchHistoryVO();
                BeanUtils.copyProperties(wxSearchHistoryVO, wxSearchHistoryPO);
                wxSearchHistoryVOS.add(wxSearchHistoryVO);
            }

            for (WxSearchIndexDefaultKeywordPO wxSearchIndexDefaultKeywordPO : hotKeywordList) {
                WxSearchIndexDefaultKeywordVO wxSearchIndexDefaultKeywordVO2 = new WxSearchIndexDefaultKeywordVO();

                BeanUtils.copyProperties(wxSearchIndexDefaultKeywordVO2, wxSearchIndexDefaultKeywordPO);
                wxSearchIndexDefaultKeywordVOS.add(wxSearchIndexDefaultKeywordVO2);
            }

        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        return new WxSearchIndexVO(wxSearchIndexDefaultKeywordVO, wxSearchHistoryVOS, wxSearchIndexDefaultKeywordVOS);

    }

    /**
     * 模糊查询helper
     * @param keyword
     * @return
     */
    @Override
    public List<String> printHelperFromKeyword(String keyword) {
        QueryWrapper<WxSearchIndexDefaultKeywordPO> keywordWrapper = new QueryWrapper<>();
        keywordWrapper.like("keyword", keyword);
        keywordWrapper.eq("deleted", 0);
        //查找按热门排序
        keywordWrapper.orderByDesc("is_hot", "update_time");
        //限制查询12条
        keywordWrapper.last("limit 12");
        List<WxSearchIndexDefaultKeywordPO> keywordList = wxSearchDefaultkeywordMapper.selectList(keywordWrapper);

        ArrayList<String> helper = new ArrayList<>();
        for (WxSearchIndexDefaultKeywordPO key : keywordList) {
            helper.add(key.getKeyword());
        }
        return helper;
    }

    @Override
    public void clearHistory(Integer userId) {
        UpdateWrapper<WxClearHistoryPO> clearWrapper = new UpdateWrapper<>();
        clearWrapper.eq("user_id", userId);
        Date date = new Date();
        WxClearHistoryPO wxClearHistoryPO = new WxClearHistoryPO(date, true);
        wxClearHistoryMapper.update(wxClearHistoryPO, clearWrapper);
    }
}
